package com.yanqu.road.server.cmd.activity.jianchuanghua;

import com.yanqu.road.pb.activity.JianChuangHuaProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.activity.jianchuanghua.JianChuangHuaModule;
import com.yanqu.road.server.manger.activity.jianchuanghua.JianChuangHuaMgr;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;

import java.util.ArrayList;
import java.util.List;

@Cmd(code = GameProtocol.S_ACTIVITY_JIAN_CHUANG_HUA_CUT, desc = "获取活动排行奖励")
public class JianChuangHuaCutCmd extends AbstractCommand {

    private int times;

    private List<Integer> rewardIdList = new ArrayList<>();

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        JianChuangHuaProto.JianChuangHuaReqMsg reqMsg = JianChuangHuaProto.JianChuangHuaReqMsg.parseFrom(packet.getBytes());
        times = reqMsg.getTimes();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        if (times != 1 && times != 10) {
            ret = GameErrorCode.E_ACTIVITY_JCH_TIMES_ERROR;
            return;
        }
        if (!JianChuangHuaMgr.isActivityInTime()) {
            ret = GameErrorCode.E_ACTIVITY_JCH_NO_IN_TIME;
        }
        JianChuangHuaModule jchModule = player.getModule(JianChuangHuaModule.class);
        int consumeCount = times == 1 ? 1 : 9;

        // 扣消耗
        if (!jchModule.reduceScissors(consumeCount)) {
            ret = GameErrorCode.E_ACTIVITY_JCH_SCISSORS_NO_ENOUGH;
            return;
        }

        // 抽
        rewardIdList = jchModule.cutByTimes(times);
    }

    @Override
    public void sendData(GamePlayer player) {
        JianChuangHuaProto.JianChuangHuaRespMsg.Builder builder = JianChuangHuaProto.JianChuangHuaRespMsg.newBuilder();
        builder.setRet(ret);
        if (ret == 0) {
            builder.addAllChuangHuaIdList(rewardIdList);
        }
        player.sendPacket(ClientProtocol.U_ACTIVITY_JIAN_CHUANG_HUA_CUT, builder);
    }
}
